﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using Newtonsoft.Json;
using System.Web.Script.Serialization;

namespace FubiManger.UI
{
    partial class Test1Form : Smobiler.Core.Controls.MobileForm
    {
        public Test1Form() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        private ms_merchant merchant = new ms_merchant();
        private void panusedit_Press(object sender, EventArgs e)
        {

        }

        private void panintegral_Press(object sender, EventArgs e)
        {

        }

        private void pan_newuser_Press(object sender, EventArgs e)
        {

        }

        private void panday_Press(object sender, EventArgs e)
        {

        }

        private void panhour_Press(object sender, EventArgs e)
        {

        }

        private void panchoosehour_Press(object sender, EventArgs e)
        {

        }

        private void btnaddclassify_Press(object sender, EventArgs e)
        {

        }

        private void dgclassview_CellBind(object sender, DragViewTemplateBindEventArgs e)
        {

        }

        private void dgclassview_CellChanged(object sender, DragViewCellChangedEventArgs e)
        {

        }

        private void button1_Press(object sender, EventArgs e)
        {
            //List<ms_order> ordlist = orderDal.QueryDelivery();
            //// 按科目分组并计算每个分组的人数
            //var ords = ordlist.GroupBy(s => s.consignee_address);
            //List<ms_order> ordlist2 = new List<ms_order>();            
            //foreach (var group in ords)
            //{
            //    NewMethod(ordlist2, group, merchant);
            //}
        }

        private static void NewMethod(List<ms_order> ordlist, IGrouping<string, ms_order> group, ms_merchant merchant)
        {
            ordlist = new List<ms_order>();
            foreach (var ms_order in group)
            {
                ms_order order = new ms_order();
                order = ms_order;
                ordlist.Add(order);
            }
            string o_ordersn = "Y" + DateTime.Now.ToString("yyyyMMddHHmmss");
            h_order ord = new h_order();
            ord.o_ordersn = o_ordersn;//订单号
            ord.o_shop_name = merchant.merchant_name;//商户名称
            ord.o_residential = ordlist[0].consignee_address;//小区
            ord.o_tel = merchant.contact_person_tel;//联系电话
            ord.o_delivery_fee = (ordlist.Count * Convert.ToDouble(merchant.distribution_costs)).ToString("f2");//配送费
            ord.o_pickup_address = merchant.address;//商家地址
            ord.o_delivery_time_start = DateTime.Now.ToString("yyyy-MM-dd" + " 11:45");//起始时间
            ord.o_delivery_time_end = DateTime.Now.ToString("yyyy-MM-dd" + " 12:45");//结束时间
            ord.o_delivery_address = ordlist[0].consignee_address;//商家地址
            ord.o_create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//创建时间            

            h_orderDal.Addord(ord);
            foreach (var ms_order in ordlist)
            {

                List<Gdpro> gdpros = new List<Gdpro>();
                string goodinfo = "";
                JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
                gdpros = js.Deserialize<List<Gdpro>>(ms_order.goods_info);
                h_order_goods good = new h_order_goods();
                good.og_oordersn = o_ordersn;
                good.og_ordersn = ms_order.order_sn;
                good.og_delivery_fee = merchant.distribution_costs;
                for (int i = 0; i < gdpros.Count; i++)
                {
                    if (i == 0)
                        goodinfo = gdpros[i].proname + "*" + gdpros[i].count;
                    else
                        goodinfo += "+" + gdpros[i].proname + "*" + gdpros[i].count;
                }
                good.og_goodsinfo = goodinfo;//商品
                good.og_tel = ms_order.consignee_tel;//收货人电话
                good.og_nickname = ms_order.consignee_name;//收货人
                good.og_delivery_address = ms_order.consignee_address;//收货地址
                good.og_paysn = ms_order.pay_sn;//支付编号
                h_orderDal.Addgoods(good);
                //orderDal.Updatestatus(ms_order.order_sn);
            }

        }

        private void Test1Form_Load(object sender, EventArgs e)
        {
            merchant = merchantDal.Querybyid("38");
        }
    }
}
